/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Portions created by the Initial Developer are
 * Copyright (C) 2012 the Initial Developer.
 * All Rights Reserved.
 *
 * The Original Code is this.
 *
 * The Initial Developer of the Original Code is the author below.
 */
/**
 * @file .h
 * @brief auto generated.
 * @author K.Ozaki
 * @date 2011/03/11
 */

#include "semi/InitialEvent.h"
#include "semi/TaskManager.h"

using namespace std;
using namespace semi;

int main(int argc, char *argv[]) {

    string opt = "";
    string tsk = "";
    string msg = "nuknown";
    string sec = "";
    long slp = 0;

    switch (argc) {
    case 5:
        sec = string(argv[4]);
        //        sec = semi::util::StringUtil::trim(sec);
        char* err;
        slp = strtol(sec.c_str(), &err, 10);
        if (0 != err && 0 != err[0])
            printf("The forth argument is invalid! %s -> %d \n", sec.c_str(),
                    slp);
        if (60 * 60 < slp) {
            printf("The forth argument is too long! %d -> %d \n", slp, 60 * 60);
            slp = 60 * 60;
        }
    case 4:
        msg = string(argv[3]);
    case 3:
        tsk = string(argv[2]);
        tsk = util::StringUtil::trim(tsk);
    case 2:
        opt = string(argv[1]);
        if ("init" == opt) {
            cout << "Initialize " << endl;
            InitialEvent::startup();
            std::cout << conf::Configurator::toString() << std::endl;
        } else if ("start" == opt) {
            if (0 < tsk.length()) {
                cout << "Start " << tsk << endl;
                TaskManager::execute(tsk);
            } else {
                cout << "Start All" << endl;
                InitialEvent::startup();
                TaskManager::executeAll();
            }
            std::cout << TaskManager::toStringCurrentTasks() << std::endl;
        } else if ("stop" == opt) {
            //            if (tsk.length() < 1) {
            //                cout << "The second argument is empty!" << endl;
            //                return -1;
            //            }
            if (0 < tsk.length()) {
                cout << "Stop " << tsk << endl;
                TaskManager::shutdown(tsk);
            } else {
                cout << "Stop All" << endl;
                TaskManager::shutdown();
            }
            std::cout << TaskManager::toStringCurrentTasks() << std::endl;
            //        } else if ("break" == opt) {
            //            cout << "Break All" << endl;
            //            TaskManager::shutdown();
        } else if ("disp" == opt) {
            cout << "Display All" << endl;
            std::cout << conf::Configurator::toString() << std::endl;
            std::cout << posix::MessageQue::toStringCurrentQue() << std::endl;
            std::cout << posix::MessageTimer::toStringCurrentTimers()
                    << std::endl;
            std::cout << TaskManager::toStringCurrentTasks() << std::endl;
        } else if ("msg" == opt) {
            if (tsk.length() < 1) {
                cout << "The second argument is empty!" << endl;
                return -1;
            }
            // check current task
            if (!TaskManager(tsk).isValid()) {
                cout << "tesk_id of second argument is not alive!" << endl;
                cout << TaskManager::toStringCurrentTasks() << endl;
                //                return -1;
            }

            semi::util::DateUtil du;
            cout << "Message send " << du.getHourString() << ":"
                    << du.getMinString() << " " << du.getSecString()
                    << " delay(sec):" << slp << endl;

            if (0 < slp)
                sleep(slp);

            //            semi::MessageEvent ev(tsk);
            //            ev.reserveMessage(0, msg); // avoid useing timer
            semi::posix::MessageQue(tsk).send(msg, 1);

            std::cout << posix::MessageQue::toStringCurrentQue() << std::endl;

        } else
            cout << "paramater error  semiframe start/stop/msg (task id)"
                    << endl;
        break;

    default:
        cout << "paramater error  semiframe start/stop/break/msg (task id)"
                << endl;
        return -1;
    }

    return 0;
}
